package com.qiandw.controller;

import com.qiandw.model.TestModel;
import com.qiandw.model.UserRegistrationForm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.security.RolesAllowed;
import java.io.StringReader;

/**
 * @author coxx
 */
@Controller
@RequestMapping(value = "test")
public class TestController {

    @RequestMapping(value = "/")
    public String getIndex(Model model){
        model.addAttribute("head", "homepage.header");
        TestModel testModel = TestModel.of("coxx", "jie", "guiyang");
        model.addAttribute("test", testModel);
        model.addAttribute("isEven", false);
        testModel.setId(12);
        model.addAttribute("row", testModel);
        return "test";
    }

    @RequestMapping(value = "register")
    public String showRegisterForm(Model model, @ModelAttribute(value = "register") UserRegistrationForm registrationForm){
        SecurityContext context = SecurityContextHolder.getContext();
        Object object = context.getAuthentication();
        System.out.println(object);
        return "redirect:test";
    }
}
